|
|
|
|
|
|
|
Let's add one more button to the BASICERR project. Set its Caption property to End and its Name property to cmdEndErr. Enter the code in Listing 14.8 to support the cmdEnd_Click event. |
|
|
|
|
|
|
|
|
Listing 14.8. Using the End keyword. |
|
|
|
|
|
|
|
|
Private Sub cmdEndErr_Click()
'
' use End to exit handler
'
On Error GoTo LocalErr
'
Dim strMsg As String
Open "junk.txt" For Input As 1
GoTo cmdEndExit
'
LocalErr:
If Err.Number = 53 Then
strMsg = "Unable to open JUNK.TXT" & vbCrLf
strMsg = strMsg & "Exit the program and check your INI file" & vbCrLf
strMsg = strMsg & "to make sure the JUNKFILE setting is correct."
MsgBox strMsg, vbCritical, "cmdEnd"
Unload Me
Else
MsgBox Str(Err) + " - " + Error$, vbCritical, "cmdEnd"
Resume Next
End If
'
LocalExit:
'
End Sub |
|
|
|
|
|
|
|
|
In Listing 14.8, you add a check in the error handler for the anticipated File not found error. You give the user some helpful information and then tell him you are closing down the program. It's always a good idea to tell the user when you are about to exit the program. Notice that you did not use the Visual Basic End keyword; you used Unload Me. Remember that End stops all program execution immediately. Using Unload Me causes Visual Basic to execute any code placed in the Unload event of the form. This event should contain any file-closing routine needed to safely exit the program. |
|
|
|
|
|
|
|
|
Save and run the project. When you click the End button, you see a message box explaining the problem and suggesting a solution (see Figure 14.7). When you click the OK button, Visual Basic ends the program. |
|
|
|
|
|